package com.woniu.smart.controller;

import com.woniu.smart.dto.EquipmentDTO;
import com.woniu.smart.entity.Equipment;
import com.woniu.smart.service.EquipmentService;
import com.woniu.smart.util.Result;
import com.woniu.smart.vo.EquipmentVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @program: smart
 * @description:
 * @author: qqq
 * @create: 2022-10-20 16:03
 **/
@RestController
@RequestMapping("equipments")
public class EquipmentController {
    @Autowired
    private EquipmentService equipmentService;
    /**
     * @Description 查询所有设备(分页，模糊查询)
     * @date  14:42:19
     * @author PC
     * @param
     * @return: com.woniu.smart.util.Result
     */
    @PostMapping("queryAll")
    public Result queryAll(@RequestBody EquipmentDTO equipmentDTO){
        equipmentDTO.setCurrentPage((equipmentDTO.getCurrentPage()-1)*equipmentDTO.getPageSize());
        List<EquipmentVO> equipmentVOList = equipmentService.selectAll(equipmentDTO);
        Long total =equipmentService.selectCount(equipmentDTO);
        return Result.ok().data("equList",equipmentVOList).data("total",total);
    }
    /**
     * @Description 根据id查找设备
     * @date  14:42:07
     * @author PC
     * @param id
     * @return: com.woniu.smart.util.Result
     */
    @GetMapping("{id}")
    public Result queryById(@PathVariable("id") Long id){
        EquipmentVO equipmentVO = equipmentService.selectById(id);
        return Result.ok().data("equVO",equipmentVO);
    }
    /**
     * @Description 添加
     * @date  14:41:47
     * @author PC
     * @param equipment
     * @return: com.woniu.smart.util.Result
     */
    @PostMapping("")
    public Result add(@RequestBody Equipment equipment){
        int i = equipmentService.insertSelective(equipment);
        if(i>0){
            return Result.ok();
        }else {
            return Result.error();
        }
    }
    /**
     * @Description 修改
     * @date  14:41:36
     * @author PC
     * @param equipment
     * @return: com.woniu.smart.util.Result
     */
    @PutMapping("")
    public Result update(@RequestBody Equipment equipment){
        int i = equipmentService.updateByPrimaryKey(equipment);
        if(i>0){
            return Result.ok();
        }else {
            return Result.error();
        }
    }
    /**
     * @Description 删除单个
     * @date  14:41:24
     * @author PC
     * @param id
     * @return: com.woniu.smart.util.Result
     */
    @DeleteMapping("{id}")
    public Result delete(@PathVariable("id") Long id){
        int i = equipmentService.deleteByPrimaryKey(id);
        if(i>0){
            return Result.ok();
        }else {
            return Result.error();
        }
    }
    /**
     * @Description 批量删除
     * @date  14:41:09
     * @author PC
     * @param
     * @return: com.woniu.smart.util.Result
     */
    @PostMapping("deleteAll")
    public Result deleteAll(@RequestBody List<EquipmentVO> equipmentVOS){
         List delList =new ArrayList();
        for (EquipmentVO equipmentVO : equipmentVOS) {
            delList.add(equipmentVO.getEquipmentId());
        }
        int i = equipmentService.deleteAll(delList);
       if (i>0){
            return Result.ok();
       }else {
           return Result.error();
       }
    }
}
